package com.lxd.property.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lxd.property.pojo.Department;
import com.lxd.property.service.DepartmentService;
import com.lxd.property.utils.Page;

@Controller
@RequestMapping("department")
public class DepartmentController {

	@Autowired
	private DepartmentService departmentService;

	@RequestMapping(value = "findAllDepartment", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findAllDepartment(Page pageUtil, Integer state1,
			@RequestParam(value = "s_name", required = false) String s_name) {
		Map<String, Object> map = new HashMap<String, Object>();
        
		map.put("start", pageUtil.getStart());
		map.put("rows", pageUtil.getRows());
		map.put("s_name", s_name);
		map.put("state1", state1);

		return departmentService.findAllDepartment(map);

	}

	@RequestMapping(value = "disableDepartment", method = RequestMethod.POST)
	@ResponseBody
	public Boolean disableDepartment(Integer id, Integer state) {

		Map<String, Object> map = new HashMap<String, Object>();
		state = state == 0 ? 1 : 0;

		map.put("id", id);
		map.put("state", state);

		return departmentService.disableDepartment(map) == 1;

	}

	@RequestMapping(value = "judgeRepeation", method = RequestMethod.POST)
	@ResponseBody
	public Boolean judgeRepeation(Department department) {
		if (department.getId() == null || department.getId().equals("")) {
			return departmentService.judgeRepeation(department) == null;
		} else {
			return (departmentService.judgeRepeation(department) == null
					|| department.getId().equals(departmentService.judgeRepeation(department).getId()));
		}
	}

	@RequestMapping(value = "addDepartment", method = RequestMethod.POST)
	@ResponseBody
	public Boolean addDepartment(Department department) {
		// TODO Auto-generated method stub
		return departmentService.addDepartment(department) == 1;
	}

	@RequestMapping(value = "findDepartmentById", method = RequestMethod.POST)
	@ResponseBody
	public Department findDepartmentById(Integer id) {
		// TODO Auto-generated method stub
		return departmentService.findDepartmentById(id);
	}

	@RequestMapping(value = "updateDepartment", method = RequestMethod.POST)
	@ResponseBody
	public Boolean updateDepartment(Department department) {
		return departmentService.updateDepartment(department) == 1;

	}

	@RequestMapping(value = "findAllDepartmentToList", method = RequestMethod.POST)
	@ResponseBody
	public List<Department> findAllDepartmentToList() {
		List<Department> list = departmentService.findAllDepartmentToList();
	    Department department = new Department();
		department.setId(null);
		return list;
	}
	
	@RequestMapping(value = "findAllDepartmentToList1", method = RequestMethod.POST)
	@ResponseBody
	public List<Department> findAllDepartmentToList1() {
		List<Department> list = departmentService.findAllDepartmentToList();
	    Department department = new Department();
		department.setId(null);
		department.setName("选择全部");
	    list.add(0, department);
		return list;
	}
	
	@RequestMapping(value = "findAllDepartmentList.do", method = RequestMethod.POST)
	@ResponseBody
	public List<Department> findAllDepartmentList() {
		List<Department> list = departmentService.findAllDepartmentList();
	    Department department = new Department();
		department.setId(null);
		department.setName("选择全部");
	    list.add(0, department);
		return list;
	}
}
